package com.handpay.zztong.hp.setting.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.handpay.framework.ClientEngine;
import com.handpay.framework.HttpEngine;
import com.handpay.framework.LuaTableUtil;
import com.handpay.framework.SecureManager;
import com.handpay.framework.utils.CommonUtils;
import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.bean.ResponseBean;
import com.handpay.zztong.hp.config.ZZTConfig;
import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.log.PlainLogger;
import com.handpay.zztong.hp.log.Storage;
import com.handpay.zztong.hp.log.StorageUtility;
import com.handpay.zztong.hp.log.SystemUtility;
import com.handpay.zztong.hp.other.activity.ShowHtmlActivity;
import com.handpay.zztong.hp.utils.AccountUtils;

import java.io.File;
import java.util.Hashtable;

import se.krka.kahlua.vm.LuaTable;

public class SettingActivity extends ZZTongActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_setting);
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        findViewById(R.id.setting_help).setOnClickListener(
                CommonUtils.createNoQuickResponseViewListener(1000,
                        new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                Intent intent1 = new Intent(
                                        SettingActivity.this, ShowHtmlActivity.class);
                                intent1.putExtra(ShowHtmlActivity.ShowHtml_Title_Key,
                                        getString(R.string.Help_center));
                                intent1.putExtra(
                                        ShowHtmlActivity.ShowHtml_Html_Path_Key,
                                        ZZTConfig.HPZZT_USERINFO_URL);
                                SettingActivity.this.startActivity(intent1);
                            }
                        }));
        findViewById(R.id.setting_version_upgrade).setOnClickListener(
                CommonUtils.createNoQuickResponseViewListener(1000,
                        new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                SettingActivity.this.startActivity(new Intent(
                                        SettingActivity.this,
                                        AboutActivity.class));
                            }
                        }));
        findViewById(R.id.bt_setting_exit).setOnClickListener(
                CommonUtils.createNoQuickResponseViewListener(1000,
                        new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                doOut();
                            }
                        }));
        findViewById(R.id.setting_version_upgrade).setOnLongClickListener(
                new View.OnLongClickListener() {

                    @Override
                    public boolean onLongClick(View arg0) {
                        if (ZZTConfig.ENV.PRINTLOG) {
                            String logFilePath = StorageUtility.combinePath(
                                    getApplicationInfo().dataDir, "plainlog.txt");
                            String logFile = PlainLogger.instance(logFilePath)
                                    .getFileAbsolutePath();
                            String tempLogFile = Storage.getAppStorage()
                                    .getExternalPath("templog.txt");
                            HPLog.e(HPLog.FDL_TAG, "logFilePath:" + logFilePath);
                            HPLog.e(HPLog.FDL_TAG, "logFile:" + logFile);
                            HPLog.e(HPLog.FDL_TAG, "tempLogFile:" + tempLogFile);
                            try {
                                StorageUtility.copyFile(new File(logFile),
                                        new File(tempLogFile));
                                Intent intent = SystemUtility
                                        .createSendEmailIntent("472477640@qq.com",
                                                "BlueOffice crash log", tempLogFile);
                                startActivity(intent);
                            } catch (Exception ex) {
                                HPLog.e("SettingFragment",
                                        "Failed to send log.", ex);
                                return false;
                            }
                        }
                        return false;
                    }
                });
    }

    /***
     * 账账通账户退出
     */
    private void doOut() {
        if (AccountUtils.getAccountPhone(this) == null)
            return;
        Hashtable<String, String> params = new Hashtable<>();
        params.put("phone", SecureManager.getInstance().des(AccountUtils.getAccountPhone(this), 1, null));
        params.put("channel", ClientEngine.CHANNEL);
        doJsonPost(this, DO_zztAccountExit, ResponseBean.class, params);
    }

    @Override
    public ActionBar getActivityActionBar() {
        return new ActionBar(getString(R.string.new_setting_title), true);
    }

    @Override
    public boolean netJsonResponse(String action, ResponseBean resp, boolean showAlert, DialogInterface.OnClickListener on) {
        if (super.netJsonResponse(action, resp, showAlert, on)) {
            return true;
        }
        if (action.equals(DO_zztAccountExit)) {
            if (resp.getResponseCode().equals("0")) {
                exitAccount();
                return false;
            }
        }
        return false;
    }

    @Override
    public boolean netResponse(String action, Hashtable<String, Object> resp,
                               boolean showAlert) {
        boolean result = super.netResponse(action, resp, showAlert);

        if (result) {
            this.setResult(Activity.RESULT_CANCELED);
            this.closeProgressDialog();
            return true;
        }

        if (action.equals(DO_zztAccountExit)) {
            int responseCode = -1;
            byte data[] = (byte[]) resp.get(HttpEngine.Key_RespData);
            LuaTable luaTable = LuaTableUtil.stringToLuaTable(data,
                    isDataCompile);
            Object rc = luaTable.rawget("responseCode");
            if (rc != null) {
                if (rc instanceof String) {
                    responseCode = Integer.parseInt((String) rc);
                } else {
                    responseCode = ((Double) rc).intValue();
                }
            }
            if (responseCode == 0) {
                exitAccount();
            }
        }
        return false;
    }
}
